package cn.edu.ujn.finaltest.controller;

import java.util.ArrayList;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.edu.ujn.finaltest.dao.BaseDict;
import cn.edu.ujn.finaltest.dao.Customer;
import cn.edu.ujn.finaltest.dao.OrderItem;
import cn.edu.ujn.finaltest.dao.Orders;
import cn.edu.ujn.finaltest.dao.Product;
import cn.edu.ujn.finaltest.dao.User;
import cn.edu.ujn.finaltest.service.IOrderItemService;
import cn.edu.ujn.finaltest.service.IOrdersService;
import cn.edu.ujn.finaltest.service.IProductService;
import cn.edu.ujn.finaltest.util.Page;

@Controller
public class OrderItemController {
	@Autowired
	IOrderItemService orderItemService;
	@Autowired
	IProductService productService;
	@Autowired
	IOrdersService ordersService;

	@RequestMapping(value = "/orderitem/list.action")
	public String list(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer rows,
			OrderItem orderItem, Model model, HttpSession httpSession) {
		// 获取登录用户的ID
		User user = (User) httpSession.getAttribute("USER_SESSION");
		Integer user_id = user.getUser_id();
		orderItem.setUser_id(user_id);
		// 根据信息查找用户的订单
		Page<OrderItem> orderItems = orderItemService.findOrderItemList(page, rows, orderItem);
		// 查询用户所有订单
		OrderItem userOrderItems = new OrderItem();
		userOrderItems.setUser_id(user_id);
		HashMap<Integer, String> orders = new HashMap<Integer, String>();
		List<OrderItem> selectUserOrderItemList = orderItemService.selectUserOrderItemList(userOrderItems);
		for (OrderItem userOrderItem : selectUserOrderItemList) {
			orders.put(userOrderItem.getOrder_id(), userOrderItem.getOrder_number());
		}
		// 查询所有产品
		List<Product> productsList = productService.findAll(null);
		HashMap<Integer, String> products = new HashMap<Integer, String>();
		for (Product product : productsList) {
			products.put(product.getId(), product.getName());
		}
		
		model.addAttribute("page", orderItems);
		model.addAttribute("orders", orders);
		model.addAttribute("products", products);
		
		return "orderitem";
	}
	
	/**
	 * 创建订单项
	 */
	@RequestMapping("/orderitem/create.action")
	@ResponseBody
	public String orderItemCreate(OrderItem orderItem) {
	    OrderItem insertOrderItem = new OrderItem();
	    // 设置插入订单项的信息
	    insertOrderItem.setOrder_id(orderItem.getOrder_id());
	    insertOrderItem.setProduct_id(orderItem.getProduct_id());
	    // 设置插入订单项的条数
	    Integer nums = orderItem.getNum();
	    // 执行Service层中的创建方法，返回的是成功插入的行数
	    int rows = this.orderItemService.insertSelectiveNums(insertOrderItem, nums);
	    if(rows == nums){
	        return "OK";
	    }else{
	        return "FAIL";
	    }
	}
	
	/**
	 * 通过order_id，获取order_id对应的产品名、产品数量等信息
	 * @param order_id
	 * @return
	 */
	@RequestMapping("/orderitem/getOrderItemById.action")
	@ResponseBody
	public OrderItem getOrderItemById(@RequestParam(value = "order_id") Integer order_id, 
			@RequestParam(value = "product_id") Integer product_id,
			HttpSession httpSession) {
		OrderItem orderItem = new OrderItem();
		// 获取用户Id，用于查询
		User user = (User) httpSession.getAttribute("USER_SESSION");
		// 设置查询参数
		orderItem.setUser_id(user.getUser_id());
		orderItem.setOrder_id(order_id);
		orderItem.setProduct_id(product_id);
		List<OrderItem> selectUserOrderItemList = this.orderItemService.selectUserOrderItemList(orderItem);
		OrderItem updateOrderItem = selectUserOrderItemList.get(0);
		// 保存修改之前的产品数，用于订单项的修改
		httpSession.setAttribute("current_nums", updateOrderItem.getNum());
	    return updateOrderItem;
	}
	
	/**
	 * 更新字典项
	 */
	@RequestMapping("/orderitem/update.action")
	@ResponseBody
	public String orderItemUpdate(OrderItem updateOrderItem, HttpSession httpSession) {
		// 根据修改后的typeName，修改为对应TypeCode
		Integer oldOrderItemNum = (Integer) httpSession.getAttribute("current_nums");
		Integer newOrderItemNum = updateOrderItem.getNum();
		OrderItem orderItem = new OrderItem();
		// 设置修改订单的相关参数
		orderItem.setOrder_id(updateOrderItem.getOrder_id());
		orderItem.setProduct_id(updateOrderItem.getProduct_id());
		if (newOrderItemNum < 0) {
			// 修改数量是负数,修改失败
			return "FAIL";
		}
	    if(oldOrderItemNum - newOrderItemNum > 0){
	        // 减少订单项
	    	int reduceNum = oldOrderItemNum - newOrderItemNum;
	    	int deleteOrderItemsNums = this.orderItemService.deleteOrderItemsNums(orderItem, reduceNum);
	    	System.out.println("减少订单数：" + deleteOrderItemsNums);
	    } else {
	        // 增加订单项
	    	int increaseNum = newOrderItemNum - oldOrderItemNum;
	    	int increaseOrderItemsNums = this.orderItemService.insertSelectiveNums(orderItem, increaseNum);
	    	System.out.println("增加订单数：" + increaseOrderItemsNums);
	    }
	    return "OK";
	}
	
	/**
	 * 删除订单项
	 */
	@RequestMapping("/orderitem/delete.action")
	@ResponseBody
	public String orderItemDelete(@RequestParam(value = "order_id") Integer order_id, 
			@RequestParam(value = "product_id") Integer product_id,
			@RequestParam(value = "num") Integer num) {
		// 设置删除订单项的参数
	    OrderItem deleteOrderitem = new OrderItem();
	    deleteOrderitem.setOrder_id(order_id);
	    deleteOrderitem.setProduct_id(product_id);
	    // 删除所有匹配该订单项的订单
	    int deleteOrderItemsNums = this.orderItemService.deleteOrderItemsNums(deleteOrderitem, num);
	    if (deleteOrderItemsNums == num) {
	    	return "OK";
	    } else {
	    	return "FAIL";
	    }
	    
	}

}
